/*
 * Copyright (c) 2012, Markus Weber All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, are
 * permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, this list of
 * conditions and the following disclaimer. Redistributions in binary form must reproduce the
 * above copyright notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 
 * Neither the name of the author nor the names of its contributors may be used to endorse or
 * promote products derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 */
package de.dfki.graph.loader;

import de.dfki.graph.*;
import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

import net.sourceforge.gxl.GXLAtomicValue;
import net.sourceforge.gxl.GXLAttr;
import net.sourceforge.gxl.GXLDocument;
import net.sourceforge.gxl.GXLEdge;
import net.sourceforge.gxl.GXLGXL;
import net.sourceforge.gxl.GXLGraph;
import net.sourceforge.gxl.GXLGraphElement;
import net.sourceforge.gxl.GXLNode;

import org.apache.log4j.Logger;
import org.xml.sax.SAXException;

/**
 * Loader for GXL format.
 *
 * @author Markus Weber
 *
 */
public class GXLLoader extends Loader {

    /**
     * Logger
     */
    private Logger log = Logger.getLogger(GXLLoader.class);

    /*
     * (non-Javadoc)
     *
     * @see de.dfki.graph.loader.Loader#load(java.io.File, java.lang.String,
     * java.lang.String)
     */
    @Override
    public Set<LabeledGraph> load(File gxlFile, String labelattribute,
            String edge_attribute) {
        Set<LabeledGraph> result = new HashSet<LabeledGraph>();
        if (gxlFile != null && gxlFile.exists()) {
            try {
                GXLDocument doc = new GXLDocument(gxlFile);
                GXLGXL el = doc.getDocumentElement();
                // There can be multiple graphs
                for (int i = 0; i < el.getGraphCount(); i++) {
                    GXLGraph g = el.getGraphAt(i);
                    // new graph
                    LabeledGraph graph = new LabeledGraph(g.getID(), new SimpleLabelnameOrder());
                    // iterate over graph elements
                    for (int j = 0; j < g.getGraphElementCount(); j++) {
                        GXLGraphElement e = g.getGraphElementAt(j);
                        if (e instanceof GXLEdge) { // edges
                            GXLEdge ed = (GXLEdge) e;
                            String iD_source = ed.getSourceID();
                            String iD_target = ed.getTargetID();
                            String label = ((GXLAtomicValue) ed.getAttr(edge_attribute).getValue()).getValue();
                            LabeledEdge<StringLabel> edge = graph.addEdge(
                                    iD_source, iD_target, new StringLabel(
                                    label != null ? label : "UNKNOWN"));
                            for (int a = 0; a < e.getAttrCount(); a++) {
                                GXLAttr att = e.getAttrAt(a);
                                if (att.getValue() instanceof GXLAtomicValue) {
                                    edge.addAttribute(att.getName(),
                                            ((GXLAtomicValue) att.getValue()).getValue());
                                }
                            }
                        } else if (e instanceof GXLNode) { // nodes
                            String str_label;
                            if (e.getAttr(labelattribute) != null) {
                                str_label = ((GXLAtomicValue) e.getAttr(
                                        labelattribute).getValue()).getValue();
                            } else { // attribute label not set
                                str_label = "UNKNOWN";
                            }
                            StringLabel label = new StringLabel(str_label);
                            LabeledVertex<StringLabel> v = graph.createVertex(
                                    e.getID(), label);
                            for (int a = 0; a < e.getAttrCount(); a++) {
                                GXLAttr att = e.getAttrAt(a);
                                if (att.getValue() instanceof GXLAtomicValue) {
                                    v.addAttribute(att.getName(),
                                            ((GXLAtomicValue) att.getValue()).getValue());
                                }
                            }
                        }

                    }
                    result.add(graph);
                }

            } catch (IOException e) {
                log.error(e);
            } catch (SAXException e) {
                log.error(e);
            }
        }
        return result;
    }
}
